package com.base.system.config.security;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * 拥有路径的拦截验证
 */
@Component
public class AuthenticationUrlFilter implements FilterInvocationSecurityMetadataSource {
    /**
     * 路径匹配
     */
    AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
     * 获取对应路径匹配角色的信息
     * @param o
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        String requestUrl = ((FilterInvocation) o).getRequestUrl();//获取请求地址
//        List<Menu> allMenus = menuService.getAllMenus();
//        for (Menu menu : allMenus) {
//            if(antPathMatcher.match(menu.getPattern(),requestUrl)){
//                //如果匹配上了
//                Set<Role> menuRoles = menu.getMenuRoles();
//                List<String> collect = CollUtil.newArrayList(menuRoles).stream().map(t -> t.getName()).collect(Collectors.toList());
//                return SecurityConfig.createList(ArrayUtil.toArray(collect,String.class));
//            }
//        }
        //如果没有匹配的表示登录之后就能访问这个
        return SecurityConfig.createList("ROLE_login");
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
